home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / xintro18.zip / XINTRO.TXT < prev   
Text File  |  1993-11-25  |  23KB  |  570 lines

  1. Title:        INTRODUCTION TO MODE X (XINTRO.TXT)
  2.  
  3. Version:        1.8
  4.  
  5. Author:        Robert Schmidt <robert@stud.unit.no>
  6.  
  7. Copyright:    (C) 1993 of Ztiff Zox Softwear - refer to Status below.
  8.  
  9. Last revision:  25-Nov-93
  10.  
  11. Figures:    1. M13ORG - memory organization in mode 13h
  12.         2. MXORG - memory organization in unchained modes
  13.  
  14.         The figures are available both as 640x480x16 bitmaps
  15.         (in GIF format), and as 7-bit ASCII text (ASC) files.
  16.  
  17. C sources:    1. LIB.C v1.2 - simple graphics library for planar,
  18.                    256-color modes - optionally self-testing.
  19.  
  20.                 Excerpts from the source(s) appear in this article.
  21.                 Whenever there are conflicts, the external source file(s)
  22.         are correct (or, at least, newest), _not_ the excerpts
  23.         provided here.
  24.  
  25. Status:        This article, its associated figures and source listings
  26.         named above, are all donated to the public domain.
  27.         Do with it whatever you like, but give credit where
  28.         credit is due.  I would prefer it if this archive was
  29.         distributed in its entirety, including the files
  30.         mentioned above.
  31.  
  32.         The standard disclaimer applies.
  33.  
  34. Index:        0. ABSTRACT
  35.         1. INTRODUCTION TO THE VGA AND ITS 256-COLOR MODE
  36.         2. GETTING MORE PAGES AND PUTTING YOUR FIRST PIXEL
  37.         3. THE ROAD FROM HERE
  38.         4. BOOKS ON THE SUBJECT
  39.                 5. BYE - FOR NOW
  40.  
  41.  
  42. 0. ABSTRACT
  43.  
  44. This text gives a fairly basic, yet technical, explanation to what, why
  45. and how Mode X is.  It first tries to explain the layout of the VGA
  46. memory and the shortcomings of the standard 320x200 256-color mode,
  47. then gives instructions on how one can progress from mode 13h to a
  48. multipage, planar 320x200 256-color mode, and from there to the
  49. quasi-standard 320x240 mode, known as Mode X.
  50.  
  51. A little experience in programming the standard VGA mode 13h
  52. (320x200 in 256 colors) is assumed.  Likewise a good understanding of
  53. hexadecimal notation and the concepts of segments and I/O ports is
  54. assumed.  Keep a VGA reference handy, which at least should have
  55. definitions of the VGA registers at bit level.
  56.  
  57. Throughout the article, a simple graphics library for unchained (planar)
  58. 256-color modes is developed.  The library supports the 320x200 and
  59. 320x240 modes, active and visible pages, and writing and reading
  60. individual pixels.
  61.  
  62.  
  63. 1. INTRODUCTION TO THE VGA AND ITS 256-COLOR MODE
  64.  
  65. Since its first appearance on the motherboards of the IBM PS/2 50, 60
  66. and 80 models in 1987, the Video Graphics Array has been the de facto
  67. standard piece of graphics hardware for IBM and compatible personal
  68. computers.  The abbreviation, VGA, was to most people synonymous with
  69. acceptable resolution (640x480 pixels), and a stunning rainbow of colors
  70. (256 from a palette of 262,144), at least compared to the rather gory
  71. CGA and EGA cards.
  72.  
  73. Sadly, to use 256 colors, the VGA BIOS limited the users to 320x200
  74. pixels, i.e. the well-known mode 13h.  This mode has one good and one
  75. bad asset.  The good one is that each one of the 64,000 pixels is easily
  76. addressable in the 64 Kb video memory segment at 0A000h.  Simply calculate
  77. the offset using this formula:
  78.  
  79. offset = (y * 320) + x;
  80.  
  81. Set the byte at this address (0A000h:offset) to the color you want, and
  82. the pixel is there.  Reading a pixel is just as simple: just read the
  83. corresponding byte.  This was heaven, compared to the havoc of planes and
  84. masking registers needed in 16-color modes.  Suddenly, the distance from a
  85. graphics algorithm on paper to an implemented graphics routine in assembly
  86. was cut down to a fraction.  The results were impressively fast, too!
  87.  
  88. The bad asset is that mode 13h is also limited to only one page, i.e.
  89. the VGA can hold only one screenful at any one time (plus 1536 pixels, or
  90. about four lines).  Most 16-color modes let the VGA hold more than one page,
  91. and this enables you to show one of the pages to the user, while drawing on
  92. another page in the meantime.  Page flipping is an important concept in making
  93. flicker free animations.  Nice looking and smooth scrolling is also almost
  94. impossible in mode 13h using plain VGA hardware.
  95.  
  96. Now, the alert reader might say: "Hold on a minute!  If mode 13h enables
  97. only one page, this means that there is memory for only one page.  But I
  98. know for a fact that all VGAs have at least 256 Kb RAM, and one 320x200
  99. 256-color page should consume only 320*200=64000 bytes, which is less
  100. than 64 Kb.  A standard VGA should room a little more than four 320x200
  101. pages!"  Quite correct, and to see how the BIOS puts this limitation on
  102. mode 13h, I'll elaborate a little on the memory organization of the VGA.
  103.  
  104. The memory is separated into four bit planes.  The reason for this stems
  105. from the EGA, where graphics modes were 16-color.  Using bit planes, the
  106. designers chose to let each pixel on screen be addressable by a single
  107. bit in a single byte in the video segment.  Assuming the palette has
  108. not been modified from the default, each plane represent one of the EGA
  109. primary colors: red, green, blue and intensity.  When modifying the bit
  110. representing a pixel, the Write Plane Enable register is set to the
  111. wanted color.  Reading is more complex and slower, since you can
  112. only read from a single plane at a time, by setting the Read Plane
  113. Select register.  Now, since each address in the video segment can
  114. access 8 pixels, and there are 64 Kb addresses, 8 * 65,536 = 524,288
  115. 16-color pixels can be accessed.  In a 320x200 16-color mode, this makes
  116. for about 8 (524,288/(320*200)) pages, in 640x480 you get nearly 2
  117. (524,288/(640*480)) pages.
  118.  
  119. In a 256-color mode, the picture changes subtly.  The designers decided
  120. to fix the number of bit planes to 4, so extending the logic above to 8
  121. planes and 256 colors does not work.  Instead, one of their goals was to
  122. make the 256-color mode as easily accessible as possible.  Comparing the
  123. 8 pixels/address in 16-color modes to the 1-to-1 correspondence of
  124. pixels and addresses of mode 13h, one can say that they have
  125. succeeded, but at a certain cost.  For reasons I am not aware of, the
  126. designers came up with the following effective, but memory-wasting
  127. scheme:
  128.  
  129. The address space of mode 13h is divided evenly across the four bit
  130. planes.  When an 8-bit color value is written to a 16-bit address in the
  131. VGA segment, a bit plane is automatically selected by the 2 least
  132. significant bits of the address.  Then all 8 bits of the data is written
  133. to the byte at the 16-bit address in the selected bitplane (have a look at
  134. figure 1).  Reading works exactly the same way.  Since the bit planes are so
  135. closely tied to the address, only every fourth byte in the video memory is
  136. accessible, and 192 Kb of a 256 Kb VGA go to waste.  Eliminating the
  137. need to bother about planes sure is convenient and beneficial, but to
  138. most people the loss of 3/4 of the total VGA memory sounds just hilarious.
  139.  
  140. To accomodate this new method of accessing video memory, the VGA
  141. designers introduced a new configuration bit called Chain-4, which
  142. resides as bit number 3 in index 4 of the Sequencer.  In 16-color modes,
  143. the default state for this bit is off (zero), and the VGA operates as
  144. described earlier.  In the VGA's standard 256-color mode, mode 13h, this
  145. bit is turned on (set to one), and this turns the tieing of bit
  146. planes and memory address on.
  147.  
  148. In this state, the bit planes are said to be chained together, thus mode
  149. 13h is often called a _chained mode_.
  150.  
  151. Note that Chain-4 in itself is not enough to set a 256-color mode -
  152. there are other registers which deals with the other subtle changes in
  153. nature from 16 to 256 colors.  But, as we now will base our work with
  154. mode X on mode 13h, which already is 256-color, we won't bother about
  155. these for now.
  156.  
  157.  
  158.  
  159. 2. GETTING MORE PAGES AND PUTTING YOUR FIRST PIXEL
  160.  
  161. The observant reader might at this time suggest that clearing the
  162. Chain-4 bit after setting mode 13h will give us access to all 256 Kb of
  163. video memory, as the two least significant bits of the byte address
  164. won't be `wasted' on selecting a bit plane.  This is correct.  You might
  165. also start feeling a little uneasy, because something tells you that
  166. you'll instantly loose t